package com.tm.springbootoss.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

import java.io.ByteArrayInputStream;
import java.util.Calendar;
import java.util.Date;
/**
 * 2024/6/26 Cjj OSS图片上传
 */
public class AliOssUtil {
    public static final  String endpoint="oss-cn-beijing.aliyuncs.com";

    public static final   String accessKeyId="LTAI5tK56yL3whovNRzBdJV9";

    public static final   String  accessKeySecret="6g7wM1nQwyZu5Nyqu3UB5A4H2Qyuld";

    //存储资源的地方
    public static final   String bucket= "cjr2003";

    //Oss客户对象
    public static OSS client;
    static {
      client= new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
    }

    /**
     * 上传资源到Oss
     */
    public static String upload(byte[] data,String name){
        //上传数据到Oss
        client.putObject(bucket,name, new ByteArrayInputStream(data));
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR,1);
        return getUrl(name,calendar.getTime());

    }

    public static String getUrl(String name,Date date){
               return client.generatePresignedUrl(bucket,name,date).toString();
    }
}
